package com.fuego.creditsystemcommodity.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuego.creditsystemcommodity.service.IHotelService;
import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.Hotel;
import org.example.utils.SystemConstants;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/hotel")
public class HotelController {

    @Resource
    public IHotelService hotelService;


    @GetMapping("/{id}")
    public Result queryHotelById(@PathVariable("id") Long id) {
        return hotelService.queryById(id);
    }


    @PostMapping
    public Result saveHotel(@RequestBody Hotel hotel) {
        // 写入数据库
        hotelService.save(hotel);
        // 返回店铺id
        return Result.ok(hotel.getId());
    }

    @PutMapping
    public Result updateHotel(@RequestBody Hotel hotel) {
        // 写入数据库
        hotelService.updateById(hotel);
        return hotelService.update(hotel);
    }


    @GetMapping("/of/type")
    public Result queryHotelByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current

    ) {
        return hotelService.queryByType(typeId,current);
    }


    @GetMapping("/of/name")
    public Result queryHotelByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Hotel> page = hotelService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }
}
